import { login, logout, getInfo } from "@/api/login";
import { getToken, setToken, removeToken, setUser, removeUser } from "@/utils/auth";
import defAva from "@/static/images/profile.jpg";
import { defineStore } from "pinia";
import { updateUserProfile } from "@/api/system/user"
import config from '@/config.js'
export interface LoginForm {
	username : string;
	password : string;
	code : string;
	uuid : string;
}
// 将token、name、avatar、userId等数据存入vueX状态管理

export const useUserStore = defineStore("user", {
	state: () => ({
		token: getToken(),
		name: "",
		avatar: "",
		userId: '',		//存储userid
		roles: Array(),
		permissions: [],
	}),
	actions: {
		// 登录
		login(userInfo : LoginForm) {
			const username = userInfo.username.trim();
			const password = userInfo.password;
			const code = userInfo.code;
			const uuid = userInfo.uuid;
			return new Promise((resolve, reject) => {
				login(username, password, code, uuid)
					.then((res : any) => {
						// console.log(res,'666666')
						setToken(res.token);
						this.token = res.token;
						resolve(null);
					})
					.catch((error) => {
						// console.log(error,'111')
						reject(error);
					});
			});
		},
		// 获取用户信息
		getInfo() {
			return new Promise((resolve, reject) => {
				getInfo()
					.then((res : any) => {
						// console.log(res, "res是个啥")
						const user = res.user;
						// console.log(user, "user")
						//判断登陆方式
						const loginMethod = uni.getStorageSync('loginMethod');
						const WechatInfo = uni.getStorageSync('weChatLoginInformation');
						if (loginMethod == 'Wechat') {
							// console.log(123456789)
							console.log(WechatInfo, '微信登录信息是啥')
							console.log(WechatInfo.userInfo, '头像')
							// console.log(user.userName, 'user.userName')
							// console.log(user.nickName, 'user.nickName')
							// console.log(WechatInfo.userInfo.nickName, 'WechatInfo.userName11')
							console.log(WechatInfo.userInfo.nickName, 'WechatInfo.nickName22')

							user.userName = WechatInfo.userInfo.nickName;
							console.log(user.userName,'user.userName333')
							user.avatar = WechatInfo.userInfo.avatarUrl;
							console.log(user.avatar,'user.avatar')
							this.avatar = user.avatar;
							this.name = user.userName;
							// console.log(this.avatar, 'avatar')
							// console.log(this.name, 'name')
							// console.log(user.userName, 'user.userName后')
							// console.log(user.nickName, 'user.nickName后')

							// updateUserProfile(user).then((res)=>{
							// 	console.log(res,'res')
							// })
							// this.avatar = WechatInfo.avatarUrl;
							// this.name = user.userName;
							// console.log(WechatInfo.nickName, '头像')
						} else {
							// console.log(user.nickName,'6666')
						}
						//用户头像，三元表达式
						if (loginMethod != 'Wechat') {
							const avatar =
								user.avatar == "" || user.avatar == null
									? defAva
									: config.baseUrl + user.avatar;		//字符拼接
							this.avatar = avatar;
							this.name = user.nickName;
						}
						if (res.roles && res.roles.length > 0) {
							// 验证返回的roles是否是一个非空数组
							this.roles = res.roles;
							this.permissions = res.permissions;
						} else {
							this.roles = ["ROLE_DEFAULT"];
						}

						this.userId = user.userId;
						//保存用户当前登陆人的userId
						setUser(res.user.userId)
						resolve(res);

						// updateUserProfile(user).then((res)=>{
						// 	console.log(res,'res')
						// })

					})
					.catch((error) => {
						reject(error);
					});
			});
		},
		// 退出系统
		logOut() {
			return new Promise<null>((resolve, reject) => {
				logout()
					.then(() => {
						this.token = "";
						this.roles = [];
						this.permissions = [];
						this.name = "";
						this.avatar = "";
						removeToken();
						removeUser();
						resolve(null);
					})
					.catch((error) => {
						reject(error);
					});
			});
		},
	},
});

export default useUserStore;